﻿Imports System.Data.SqlClient

Public Class clsPlanOperacion
    Private mintIdPlan As Integer
    Private mintIdRuta As Integer
    Private mintTipoDia As Integer
    Private mdblDespachos As Double
    Private mdblVueltas As Double
    Private mintAutobuses As Integer
    Private mstrUsuario As String
    Private mdatFechaUltimoCambio As Date
    Public Property IdPlan() As Integer
        Get
            Return mintIdPlan
        End Get
        Set(ByVal value As Integer)
            mintIdPlan = value
        End Set
    End Property
    Public Property IdRuta() As Integer
        Get
            Return mintIdRuta
        End Get
        Set(ByVal value As Integer)
            mintIdRuta = value
        End Set
    End Property
    Public Property TipoDia() As Integer
        Get
            Return mintTipoDia
        End Get
        Set(ByVal value As Integer)
            mintTipoDia = value
        End Set
    End Property
    Public Property Despachos() As Double
        Get
            Return mdblDespachos
        End Get
        Set(ByVal value As Double)
            mdblDespachos = value
        End Set
    End Property
    Public Property Vueltas() As Double
        Get
            Return mdblVueltas
        End Get
        Set(ByVal value As Double)
            mdblVueltas = value
        End Set
    End Property
    Public Property Autobuses() As Integer
        Get
            Return mintAutobuses
        End Get
        Set(ByVal value As Integer)
            mintAutobuses = value
        End Set
    End Property
    Public Property Usuario() As String
        Get
            Return mstrUsuario
        End Get
        Set(ByVal value As String)
            mstrUsuario = value
        End Set
    End Property
    Public Property FechaUltimoCambio() As Date
        Get
            Return mdatFechaUltimoCambio
        End Get
        Set(ByVal value As Date)
            mdatFechaUltimoCambio = value
        End Set
    End Property
    Public Sub New()
        mintIdPlan = 0
        mintIdRuta = 0
        mintTipoDia = 0
        mdblDespachos = 0
        mdblVueltas = 0
        mintAutobuses = 0
        mstrUsuario = ""
        mdatFechaUltimoCambio = Now()
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into PlanOperacion (" & _
                            "IdRuta," & _
                            "TipoDia," & _
                            "Despachos," & _
                            "Vueltas," & _
                            "Autobuses," & _
                            "Usuario," & _
                            "FechaUltimoCambio) " & _
                            "VALUES (" & _
                            "@IdRuta," & _
                            "@TipoDia," & _
                            "@Despachos," & _
                            "@Vueltas," & _
                            "@Autobuses," & _
                            "substring(@Usuario,1,20)," & _
                            "@FechaUltimoCambio) "


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("TipoDia", mintTipoDia)
        Command.Parameters.AddWithValue("Despachos", mdblDespachos)
        Command.Parameters.AddWithValue("Vueltas", mdblVueltas)
        Command.Parameters.AddWithValue("Autobuses", mintAutobuses)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("FechaUltimoCambio", mdatFechaUltimoCambio)
        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el plan de operación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update PlanOperacion Set " & _
                    "Despachos=@Despachos," & _
                    "Vueltas=@Vueltas," & _
                    "Autobuses=@Autobuses," & _
                    "Usuario=substring(@Usuario,1,20)," & _
                    "FechaUltimoCambio=@FechaUltimoCambio " & _
                    "Where IdPlan=@IdPlan"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdPlan", mintIdPlan)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("TipoDia", mintTipoDia)
        Command.Parameters.AddWithValue("Despachos", mdblDespachos)
        Command.Parameters.AddWithValue("Vueltas", mdblVueltas)
        Command.Parameters.AddWithValue("Autobuses", mintAutobuses)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("FechaUltimoCambio", mdatFechaUltimoCambio)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el plan de operación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
